/*
 * Copyright (C) 2005, Velocityme Partnership
 * DirectoryActions.java
 *
 * Created on 24 July 2005, 04:41
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.session.ContactableLocal;
import com.velocityme.session.KeyLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class DirectoryActions extends NodeActions {

    /** Creates a new instance of DirectoryActions */
    public DirectoryActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("selectedTab", "Directory");
        return super.show(mapping, form, request, response);
    }

    @Override
    public ActionForward showNode(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("directoryDTO", lookupContactableBean().getDirectoryDTO(key));
        return mapping.findForward("ACTION_DISPLAY");
    }

    private ContactableLocal lookupContactableBean() {
        try {
            Context c = new InitialContext();
            return (ContactableLocal) c.lookup("java:comp/env/ContactableBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
